<template>
  <PageWrapper contentClass="flex">
    <BasicTable @register="registerTable" @row-click="handleSelect" class="w-2/5 menu">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增菜单 </a-button>
      </template>
      <template #name="{ record }"> <Icon :icon="record.icon" /> {{ record.name }} </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'ant-design:plus-circle-outlined',
              onClick: handleCreate.bind(null, record),
            },
            {
              icon: 'clarity:note-edit-line',
              onClick: handleUpdate.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <MenuPermsTable ref="permsTableRef" class="w-3/5 menu-button-table" />
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { useDrawer } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { Icon } from '/@/components/Icon';
  import { PageWrapper } from '/@/components/Page';

  import { getMenuList, deleteById } from './menu.api';
  import { columns, searchFormSchema } from './menu.data';
  import MenuPermsTable from './MenuPermsTable.vue';
  import MenuDrawer from './MenuDrawer.vue';

  export default defineComponent({
    name: 'MenuManagement',
    components: { BasicTable, TableAction, Icon, PageWrapper, MenuDrawer, MenuPermsTable },
    setup() {
      const { createConfirm } = useMessage();
      const permsTableRef = ref();

      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: '菜单列表',
        api: getMenuList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        pagination: false,
        striped: false,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleCreate(record: Recordable) {
        const pid = record?.id || '0';
        const appCode = record?.appCode || '';
        openDrawer(true, { record: { parentId: pid, appCode: appCode }, isUpdate: false });
      }

      function handleUpdate(record: Recordable) {
        openDrawer(true, { record, isUpdate: true });
      }

      function handleDelete(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: '确认',
          content: `确定删除 ${record.label} ？ 同时会级联删除子节点以及相关资源数据`,
          onOk: async () => {
            deleteById({ id: record.id }, handleSuccess);
          },
        });
      }

      function handleSuccess() {
        reload();
      }

      function handleSelect(record: Recordable) {
        permsTableRef.value.filterByParentId(record);
      }

      return {
        registerTable,
        registerDrawer,
        permsTableRef,
        handleCreate,
        handleUpdate,
        handleDelete,
        handleSuccess,
        handleSelect,
      };
    },
  });
</script>
